Computer memories take many different forms. The slowest memories, and usually the farthest from the central processing unit (CPU), are virtual memories, such as a disk drive. The fastest, and usually the nearest, memories, include main memory, registers, and caches. Of these, main memory is the slowest. Since caches are closer to the CPU, they allow information, such as recently used instructions and/or data, to be quickly accessed by the CPU. However, since caches are relatively expensive, and limited in size, the data that can be stored on a cache memory is limited.
Cache memories can be used to store read-only data and read/write data. When read-only data is cached, data that is read from a memory can be stored in a cache so that the next time the data needs to be read, it can be read from the faster cache rather than the slower system memory. An example of read-only data that is cache read from memory is instructions to a program. Read-only data is not prone to data loss since any data that gets corrupted in the cache can always be read from memory.
When read/write data is cached, data that is to be written to system memory can be written to the cache instead. The way that a cache handles writes is called the “write policy” of the cache. There are different write policies, two of which are described below.
A cache can be a write-back cache. When a write is made to system memory at a location that is currently cached, the new data is only written to the cache, and not to the system memory. Correspondingly, if another memory location needs to use the cache line where this data is stored, the currently stored data needs to be saved—i.e., written back—to the system memory so that the line can be used by the new memory location.
A cache can, alternatively, be a write-through cache. With this method, everytime a processor writes to a cached memory location, both the cache and the underlying memory location are updated. Cache written data may include any data, for example, a user-modified document.
While the write-through policy is more recoverable than the write back policy with respect to recovering from cache corruption, the write-back policy provides better performance at the risk of memory integrity, and are, therefore, prone to data loss due to cache errors.
In particular, where the write-back policy is utilized (i.e., new data that is to be written to a memory location that is cached is written to the cache and not to the memory location, and the new data is saved—written back to memory—when the cache line needs to be used by another memory location, for example), there is the risk that the cache may be corrupted before new data is saved.
Data in the cache which matches the data in a corresponding memory location is called clean data; and data in the cache which does not match the data in a corresponding memory location is called dirty data.